/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FinisherLoanComputerManual.java
 *
 * Created on 20/06/2011, 10:52:17 AM
 */

package GUI.ATTENDANT;

import GUI.Customer.Monitor;
import GUI.Customer.Server;
import Procedures.DBConection;
import Procedures.LoanTable;
import entities.Loan;
import java.util.Calendar;
import javax.swing.JFrame;

/**
 *
 * @author SIX-14
 */
public class FinisherLoanComputerManual extends javax.swing.JPanel {

    /** Creates new form FinisherLoanComputerManual */
    public FinisherLoanComputerManual(DBConection myConection, JFrame paternFrame, LoanTable loantable, Server server) {
        initComponents();
        this.myConection=myConection;
        this.paternFrame=paternFrame;
        this.loanTable=loantable;
        this.server=server;
        fillcomboboxes();
    }


    private DBConection myConection;
    private JFrame paternFrame;
    private LoanTable loanTable;
    private int numberComputer;
    private Server server;
    private Monitor monitor;
    private Loan loan;

    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
    }

    public Monitor getMonitor() {
        return monitor;
    }

    public void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    public int getNumberComputer() {
        return numberComputer;
    }

    public void setNumberComputer(int numberComputer) {
        this.numberComputer = numberComputer;
    }

    public LoanTable getLoanTable() {
        return loanTable;
    }

    public void setLoanTable(LoanTable loanTable) {
        this.loanTable = loanTable;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }

    public void fillcomboboxes(){
        getMyConection().sendSQLQuery("SELECT name FROM TYPE_USER;");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox1.removeAllItems();
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jButton1.setText("Buscar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Escriba identificador del Usuario");

        jLabel2.setText("Sleccione el tipo de usuario");

        jLabel3.setText("Usuario esta en la Computadora: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jButton1)))
                .addContainerGap(216, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(19, 19, 19)
                .addComponent(jButton1)
                .addContainerGap(98, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(!jTextField1.getText().isEmpty()){
            if(jButton1.getText().equals("Buscar")){
                if(getLoanTable().find_user(Integer.parseInt(jTextField1.getText()),jComboBox1.getSelectedItem().toString() )){
                    jButton1.setText("Terminar");
                }
            }else{
                
                String computerRoom=null;
                String ip=null;
                for(int i=0; i<getLoanTable().getTable().length; i++){
                    if((getLoanTable().getTable()[i].getId()==Integer.parseInt(jTextField1.getText()))&&(getLoanTable().getTable()[i].getType_user().equals(jComboBox1.getSelectedItem().toString()))){
                        computerRoom=getLoanTable().getTable()[i].getComputerRoom();
                        ip=getLoanTable().getTable()[i].getIpcomputer();
                        System.out.println(ip);
                        System.out.println("x");
                    break;
                    }
                }
                if(ip!=null){
                    setLoan(getLoanTable().removeLoan(Integer.parseInt(jTextField1.getText()),"Sala",computerRoom));
                setMonitor(getServer().getComstumer().receiveResult("/"+ip));
                String endTime=(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)+":"+Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND));
                getMyConection().sendSQL("INSERT INTO LOAN_COMPUTER SET DATE='"+getLoan().getDate()+"', ID_USER="+getLoan().getId()+", NUMBER_COMPUTER="+getLoan().getNumberComputer()+", STARTTIME='"+getLoan().getStartTime()+"', ENDTIME='"+endTime+"', DURATION="+getMonitor().getDuration()+";");
                for(int i=0; i<getMonitor().getProcess().length; i++){
                    if(getMonitor().getProcess()[i].getDuration()>0)
                getMyConection().sendSQL("INSERT INTO LOAN_COMPUTER_APLICATION SET COMPUTER="+getLoan().getNumberComputer()+", ID_USER="+getLoan().getId()+", DATE='"+getLoan().getDate()+"', DURATION="+getMonitor().getProcess()[i].getDuration()+", APLICATION='"+getMonitor().getProcess()[i].getName()+"';");
                }
                
                getMyConection().sendSQL("UPDATE COMPUTER SET STATE=1 WHERE NUMBER="+getNumberComputer()+";");
                this.setVisible(false);
                getPaternFrame().getContentPane().remove(this);
                }else{System.out.println("chafa");}
            }
        }

    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
